/**
 * @file lv_vietnamese_ime.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_vietnamese_ime.h"
#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define MY_CLASS    &lv_100ask_vietnamese_ime_class

uint16_t py_page;           /* Current pinyin map pages */
uint16_t py_num[27];        /* Number and length of Pinyin */  //第26个存储shift 开头的 偏移和数量
uint16_t py_pos[27];        /* Pinyin position */


/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static lv_100ask_dict_t * vietnamese_search_matching_dict(lv_obj_t * obj, char * strInput_py_str, uint16_t * cand_num);
/**********************
 *  STATIC VARIABLES
 **********************/

                                                

#define LV_100ASK_vietnamese_IME_DEFAULT_PIN_YIN_DICT 1
#if LV_100ASK_vietnamese_IME_DEFAULT_PIN_YIN_DICT
lv_100ask_dict_t lv_100ask_def_vietnamese_dict[] = {
            {"a", "a"},  
            {"aa", "â"},  
            {"aaf", "ầ"}, 
            {"aaj", "ậ"},  
            {"aar", "ẩ"}, 
            {"aas", "ấ"}, 
            {"aax", "ẫ"}, 
            {"af", "à"}, 
            {"aj", "ạ"},  
            {"ar", "ả"}, 
            {"as", "á"}, 
            {"aw", "ă"},  
            {"awf", "ằ"}, 
            {"awj", "ặ"},  
            {"awr", "ẳ"}, 
            {"aws", "ắ"}, 
            {"awx", "ẵ"}, 
            {"ax", "ã"}, 
            {"b", "b"},  
            {"c", "c"},  
            {"d", "d"},  
            {"dd", "đ"},  
            {"e", "e"},  
            {"ee", "ê"},  
            {"eef", "ề"}, 
            {"eej", "ệ"},  
            {"eer", "ể"}, 
            {"ees", "ế"}, 
            {"eex", "ễ"}, 
            {"ef", "è"}, 
            {"ej", "ẹ"},  
            {"er", "ẻ"}, 
            {"es", "é"}, 
            {"ex", "ẽ"}, 
            {"g", "g"},  
            {"h", "h"},  
            {"i", "i"},  
            {"if", "ì"}, 
            {"ij", "ị"},  
            {"ir", "ỉ"}, 
            {"is", "í"}, 
            {"ix", "ĩ"}, 
            {"k", "k"},  
            {"l", "l"},  
            {"m", "m"},  
            {"n", "n"},  
            {"o", "o"},  
            {"of", "ò"}, 
            {"oj", "ọ"},  
            {"oo", "ô"},  
            {"oof", "ồ"}, 
            {"ooj", "ộ"},  
            {"oor", "ổ"}, 
            {"oos", "ố"}, 
            {"oox", "ỗ"}, 
            {"or", "ỏ"}, 
            {"os", "ó"}, 
            {"ow", "ơ"},  
            {"owf", "ờ"}, 
            {"owj", "ợ"},  
            {"owr", "ở"}, 
            {"ows", "ớ"}, 
            {"owx", "ỡ"}, 
            {"ox", "õ"}, 
            {"p", "p"},  
            {"q", "q"},  
            {"r", "r"},  
            {"s", "s"},  
            {"t", "t"},  
            {"u", "u"},  
            {"uf", "ù"}, 
            {"uj", "ụ"},  
            {"ur", "ủ"}, 
            {"us", "ú"}, 
            {"uw", "ư"},  
            {"uwf", "ừ"}, 
            {"uwj", "ự"},
            {"uwr", "ử"}, 
            {"uws", "ứ"}, 
            {"uwx", "ữ"}, 
            {"ux", "ũ"}, 
            {"v", "v"},  
            {"x", "x"},  
            {"y", "y"},  
            {"yf", "ỳ"}, 
            {"yj", "ỵ"},  
            {"yr", "ỷ"}, 
            {"ys", "ý"}, 
            {"yx", "ỹ"}, 
            {NULL, NULL}
};
#endif





void vietnamese_input_proc(lv_obj_t * obj)
{
    lv_100ask_pinyin_ime_t * vietnamese_ime = (lv_100ask_pinyin_ime_t *)obj;
    printf("=====vietnamese_input_proc====\n");
    printf("vietnamese_ime->input_char:%s\n",vietnamese_ime->input_char);
    lv_100ask_dict_t *cand_dict = vietnamese_search_matching_dict(obj, vietnamese_ime->input_char, &vietnamese_ime->cand_num);
    if (cand_dict == NULL) 
    {
        return;
    }
    printf("dict:%s.%s,%s\n",cand_dict->py,cand_dict->py_mb,cand_dict->py_hz);
    vietnamese_ime->py_page = 0;
printf("=====vietnamese_input_proc2====\n");
    for(uint8_t i = 0; i < LV_100ASK_PINYIN_IME_CAND_TEXT_NUM; i++)
    {
        printf("=====loop %d====\n",i);
        memset(vietnamese_ime->btnm_pinyin_sel_user[i], 0x00, CAND_STR_MAX_LEN);
        vietnamese_ime->btnm_pinyin_sel_user[i][0] = ' ';
    }
    printf("=====vietnamese_input_proc3====\n");
    char* p = cand_dict->py_hz;
    // fill buf
    if(strlen(cand_dict->py_mb)<=CAND_STR_MAX_LEN)
        strcpy(vietnamese_ime->btnm_pinyin_sel_user[0],cand_dict->py_mb);
    else
        strncpy(vietnamese_ime->btnm_pinyin_sel_user[0],cand_dict->py_mb,CAND_STR_MAX_LEN);
    printf("=====vietnamese_input_proc5====\n");
    //for(uint8_t i = 0; (i < vietnamese_ime->cand_num && i < LV_100ASK_vietnamese_IME_CAND_TEXT_NUM); i++)
    lv_obj_clear_flag(vietnamese_ime->cand_panel, LV_OBJ_FLAG_HIDDEN);
}


void init_vietnamese_dict(lv_obj_t * obj, lv_100ask_dict_t * dict)
{
    char headletter = 'a';
    uint16_t offset_sum = 0;
    uint16_t offset_count = 0;
    uint16_t letter_calc = 0;

    for(uint16_t i = 0; ; i++)
    {
        if ((NULL == (dict[i].py)) || (NULL == (dict[i].py_mb)))
        {
            headletter = dict[i-1].py[0];
            letter_calc = headletter - 'a';
            py_num[letter_calc] = offset_count;
            break;
        }   
        if (headletter == (dict[i].py[0]))
        {
            offset_count++;
        }
        else
        {
            headletter = dict[i].py[0];
            letter_calc = headletter - 'a';
            py_num[letter_calc - 1] = offset_count;
            offset_sum += offset_count;
            py_pos[letter_calc] = offset_sum;

            offset_count = 1;
        }       
    }
}

static lv_100ask_dict_t * vietnamese_search_matching_dict(lv_obj_t * obj, char * strInput_py_str, uint16_t * cand_num)
{
    lv_100ask_pinyin_ime_t * vietnamese_ime = (lv_100ask_pinyin_ime_t *)obj;
    lv_100ask_dict_t * cpHZ;
    uint8_t i, cInputStrLength = 0, offset;
    volatile uint8_t count = 0;

    if(*strInput_py_str == '\0')    return NULL;
    //if(*strInput_py_str == 'i')     return NULL;
    //if(*strInput_py_str == 'u')     return NULL;
    //if(*strInput_py_str == 'y')     return NULL;
    offset = strInput_py_str[0] - 'a';
    cInputStrLength = strlen(strInput_py_str);
    
    cpHZ  = &lv_100ask_def_vietnamese_dict[py_pos[offset]];
    count = py_num[offset];

    while(count--)
    {
        for(i = 0; i < cInputStrLength; i++)
        {
            if(*(strInput_py_str + i) != *((cpHZ->py) + i))
            {
                break;
            }
        }
        // perfect match
        if(cInputStrLength == 1 || i == cInputStrLength)
        {
            // The Chinese character in UTF-8 encoding format is 3 bytes
            * cand_num = strlen((const char*)(cpHZ->py_mb)) / 3;
            return cpHZ;
        }       
        cpHZ++;
    }
    return NULL;
}
